import axiosInstance from "./axiosInstance.ts";

export interface ApiResult<T> {
    code: number;
    message: string;
    data: T;
}
export async function get<T>(url: string, params?: any): Promise<ApiResult<T>> {
    const response = await axiosInstance.get<ApiResult<T>>(url, { params });
    return response.data;
}
export async function post<T>(url: string, data?: any): Promise<ApiResult<T>> {
    const response = await axiosInstance.post<ApiResult<T>>(url, data);
    return response.data;
}
export async function put<T>(url: string, data?: any): Promise<ApiResult<T>> {
    const response = await axiosInstance.put<ApiResult<T>>(url, data);
    return response.data;
}
export async function del<T>(url: string, params?: any): Promise<ApiResult<T>> {
    const response = await axiosInstance.delete<ApiResult<T>>(url, { params });
    return response.data;
}


// 使用示例
// try {
//     const response = await get('/users');
//     console.log(response.data);
// } catch (error) {
//     console.error(error);
// }
// try {
//     const response = await post('/users', { name: 'John', age: 30 });
//     console.log(response.data);
// } catch (error) {
//     console.error(error);
// }